{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fmm import FastMapMatch,Network,NetworkGraph,UBODTGenAlgorithm,UBODT,FastMapMatchConfig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read network and graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nodes 649 edges 1308\n"
     ]
    }
   ],
   "source": [
    "network = Network(\"stockholm/edges.shp\",\"fid\", \"u\", \"v\")\n",
    "print \"Nodes {} edges {}\".format(network.get_node_count(),network.get_edge_count())\n",
    "graph = NetworkGraph(network)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Precompute UBODT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status: success\n",
      "Time takes 0.587 seconds\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Can be skipped if you already generated an ubodt file\n",
    "ubodt_gen = UBODTGenAlgorithm(network,graph)\n",
    "# The delta is defined as 3 km approximately. 0.03 degrees. \n",
    "status = ubodt_gen.generate_ubodt(\"stockholm/ubodt.txt\", 0.03, binary=False, use_omp=True)\n",
    "# Binary is faster for both IO and precomputation\n",
    "# status = ubodt_gen.generate_ubodt(\"stockholm/ubodt.bin\", 0.03, binary=True, use_omp=True)\n",
    "print status"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create FMM model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "### Read UBODT\n",
    "ubodt = UBODT.read_ubodt_csv(\"stockholm/ubodt.txt\")\n",
    "### Create FMM model\n",
    "fmm_model = FastMapMatch(network,graph,ubodt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 8\n",
    "radius = 0.003\n",
    "gps_error = 0.0005\n",
    "fmm_config = FastMapMatchConfig(k,radius,gps_error)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run map matching for wkt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opath  [37, 957, 984, 203, 1263, 545, 1059, 1188]\n",
      "Cpath  [37, 289, 477, 254, 957, 558, 407, 395, 185, 134, 988, 984, 675, 1230, 203, 814, 1261, 1263, 577, 584, 545, 1059, 818, 1188]\n",
      "WKT  LINESTRING(18.055113 59.335538,18.055046 59.335564,18.054878 59.335596,18.054529 59.335681,18.054375 59.335721,18.054186 59.335759,18.053463 59.336012,18.053228 59.336087,18.053046 59.336122,18.052861 59.336111,18.051516 59.33605,18.051402 59.33605,18.051291 59.336057,18.051171 59.336099,18.051076 59.336187,18.050981 59.336279,18.050599 59.336665,18.050422 59.336893,18.050374 59.336942,18.050319 59.337004,18.050297 59.33703,18.050276 59.337055,18.050179 59.337166,18.049859 59.33742,18.049394 59.337886,18.049307 59.337973,18.049229 59.338051,18.048391 59.338891,18.048373 59.338909,18.048267 59.339016,18.048188 59.339096,18.048167 59.339117,18.047976 59.33931,18.047688 59.339607,18.047584 59.339706,18.047113 59.340184,18.047023 59.340275,18.046998 59.3403,18.046971 59.340327,18.045839 59.341474,18.04576 59.341548,18.04601 59.341607,18.047251 59.341928,18.047403 59.341979,18.047558 59.342024,18.048616 59.342286,18.048838 59.342341,18.049049 59.342394,18.049909 59.342602,18.05085 59.342832,18.050932 59.342851,18.050986 59.342865,18.051069 59.342884,18.052275 59.343147,18.053487 59.34345,18.053572 59.343472,18.054028 59.343589,18.055211 59.34387,18.055328 59.343899,18.055498 59.343941,18.055564 59.343873,18.056331 59.343059,18.0571 59.342243,18.057759 59.341543,18.057802 59.341498,18.057908 59.341385,18.05873 59.340513,18.05881 59.340429,18.058899 59.340333,18.05922 59.339993)\n"
     ]
    }
   ],
   "source": [
    "wkt = \"LINESTRING(18.05522129697668 59.335820514008,18.050801016520136 59.33614881818502,18.04796860380041 59.33905964310512,18.046037413309687 59.341029232754444,18.049084402750605 59.34262670491522,18.052775122355097 59.34328317853977,18.056809164713496 59.34273611806721,18.059298254679316 59.34006633652867)\"\n",
    "result = fmm_model.match_wkt(wkt,fmm_config)\n",
    "### Print map matching result\n",
    "print \"Opath \",list(result.opath)\n",
    "print \"Cpath \",list(result.cpath)\n",
    "print \"WKT \",result.mgeom.export_wkt()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create stmatch model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fmm import Network,NetworkGraph,STMATCH,STMATCHConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "stmatch_model = STMATCH(network,graph)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 4\n",
    "gps_error = 0.0005\n",
    "radius = 0.003\n",
    "vmax = 0.0003\n",
    "factor = 1.5\n",
    "stmatch_config = STMATCHConfig(k, radius, gps_error, vmax, factor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opath  [37, 957, 984, 203, 1263, 545, 1059, 1188]\n",
      "Cpath  [37, 289, 477, 254, 957, 558, 407, 395, 185, 134, 988, 984, 675, 1230, 203, 814, 1261, 1263, 577, 584, 545, 1059, 818, 1188]\n",
      "WKT  LINESTRING(18.055113 59.335538,18.055046 59.335564,18.054878 59.335596,18.054529 59.335681,18.054375 59.335721,18.054186 59.335759,18.053463 59.336012,18.053228 59.336087,18.053046 59.336122,18.052861 59.336111,18.051516 59.33605,18.051402 59.33605,18.051291 59.336057,18.051171 59.336099,18.051076 59.336187,18.050981 59.336279,18.050599 59.336665,18.050422 59.336893,18.050374 59.336942,18.050319 59.337004,18.050297 59.33703,18.050276 59.337055,18.050179 59.337166,18.049859 59.33742,18.049394 59.337886,18.049307 59.337973,18.049229 59.338051,18.048391 59.338891,18.048373 59.338909,18.048267 59.339016,18.048188 59.339096,18.048167 59.339117,18.047976 59.33931,18.047688 59.339607,18.047584 59.339706,18.047113 59.340184,18.047023 59.340275,18.046998 59.3403,18.046971 59.340327,18.045839 59.341474,18.04576 59.341548,18.04601 59.341607,18.047251 59.341928,18.047403 59.341979,18.047558 59.342024,18.048616 59.342286,18.048838 59.342341,18.049049 59.342394,18.049909 59.342602,18.05085 59.342832,18.050932 59.342851,18.050986 59.342865,18.051069 59.342884,18.052275 59.343147,18.053487 59.34345,18.053572 59.343472,18.054028 59.343589,18.055211 59.34387,18.055328 59.343899,18.055498 59.343941,18.055564 59.343873,18.056331 59.343059,18.0571 59.342243,18.057759 59.341543,18.057802 59.341498,18.057908 59.341385,18.05873 59.340513,18.05881 59.340429,18.058899 59.340333,18.05922 59.339993)\n"
     ]
    }
   ],
   "source": [
    "wkt = \"LINESTRING(18.05522129697668 59.335820514008,18.050801016520136 59.33614881818502,18.04796860380041 59.33905964310512,18.046037413309687 59.341029232754444,18.049084402750605 59.34262670491522,18.052775122355097 59.34328317853977,18.056809164713496 59.34273611806721,18.059298254679316 59.34006633652867)\"\n",
    "result = model.match_wkt(wkt,fmm_config)\n",
    "### Print map matching result\n",
    "print \"Opath \",list(result.opath)\n",
    "print \"Cpath \",list(result.cpath)\n",
    "print \"WKT \",result.mgeom.export_wkt()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Match GPS data stored in a file¶"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fmm import GPSConfig,ResultConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gps file : stockholm/trips.csv\n",
      "id column : id\n",
      "geom column : geom\n",
      "timestamp column : timestamp\n",
      "x column : x\n",
      "y column : y\n",
      "GPS point : false\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Define input data configuration\n",
    "input_config = GPSConfig()\n",
    "input_config.file = \"stockholm/trips.csv\"\n",
    "input_config.id = \"id\"\n",
    "print input_config.to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Result file : stockholm/mr.txt\n",
      "Output fields: opath cpath mgeom \n"
     ]
    }
   ],
   "source": [
    "# Define output configuration\n",
    "result_config = ResultConfig()\n",
    "result_config.file = \"stockholm/mr.txt\"\n",
    "result_config.output_config.write_opath = True\n",
    "print result_config.to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status: success\n",
      "Time takes 0.001 seconds\n",
      "Total points 14 matched 14\n",
      "Total trajectories 2 matched 2\n",
      "Map match percentage 1\n",
      "Map match speed 14000 points/s \n",
      "\n"
     ]
    }
   ],
   "source": [
    "status = fmm_model.match_gps_file(input_config, result_config, fmm_config, use_omp = True)\n",
    "print status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status: success\n",
      "Time takes 0.009 seconds\n",
      "Total points 14 matched 14\n",
      "Map match speed 1555.56 points/s \n",
      "\n"
     ]
    }
   ],
   "source": [
    "status = stmatch_model.match_gps_file(input_config, result_config, stmatch_config, use_omp = True)\n",
    "print status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
